package com.ruoyi.project.mahjong.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.mahjong.domain.dto.CostDto;
import com.ruoyi.project.mahjong.domain.dto.CostThreeDto;
import com.ruoyi.project.mahjong.domain.vo.CostThreeVo;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.TCost;
import com.ruoyi.project.mahjong.service.ITCostService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 计费Controller
 *
 * @author ruoyi
 * @date 2023-10-09
 */
@RestController
@RequestMapping("/mahjong/cost")
public class TCostController extends BaseController
{
    @Autowired
    private ITCostService tCostService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询计费列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:cost:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCost tCost)
    {
        startPage();
        List<TCost> list = tCostService.selectTCostList(tCost);
        return getDataTable(list);
    }

    @PostMapping("/saveThreeConfig")
    public AjaxResult saveThreeConfig(@RequestBody CostThreeDto costThreeDto)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        costThreeDto.setShopId(shopId);
        tCostService.saveThreeConfig(costThreeDto);


        return AjaxResult.success();
    }

    @GetMapping("/threeConfig/{roomId}")
    public AjaxResult threeConfig(@PathVariable String roomId)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        CostThreeVo costThreeVo=tCostService.threeConfig(shopId,roomId);

        return AjaxResult.success(costThreeVo);
    }
    /**
     * 查询按照分钟缴费
     */
    @GetMapping("/selectByMinute")
    public AjaxResult selectByMinute(CostDto costDto)
    {
        if (ObjectUtil.isNull(costDto)){
            AjaxResult.error("暂无设置计费信息");
        }
        TCost cost=new TCost();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        if (costDto.getRoomId()==null){
            AjaxResult.error("请先设置场地信息");

        }
        cost.setBak1(costDto.getRoomId().toString());
        cost.setBak2(shopId.toString());
        cost.setCostId(costDto.getCostId());
        List<TCost> list = tCostService.selectTCostList(cost);
        return AjaxResult.success(ObjectUtil.isNotEmpty(list)&&list.size()!=0?list.get(0):new TCost());
    }

    /**
     * 查询按照分钟缴费
     */
    @GetMapping("/selectBysite")
    public AjaxResult selectBysite(CostDto costDto)
    {
        if (ObjectUtil.isNull(costDto)){
            AjaxResult.error("暂无设置计费信息");
        }
        TCost cost=new TCost();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        cost.setBak1(costDto.getRoomId().toString());
        cost.setBak2(shopId.toString());
        cost.setCostId(costDto.getCostId());
        List<TCost> list = tCostService.selectTCostList(cost);
        return AjaxResult.success(ObjectUtil.isNotEmpty(list)&&list.size()!=0?list.get(0):new TCost());
    }
    /**
     * 导出计费列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:cost:export')")
    @Log(title = "计费", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TCost tCost)
    {
        List<TCost> list = tCostService.selectTCostList(tCost);
        ExcelUtil<TCost> util = new ExcelUtil<TCost>(TCost.class);
        return util.exportExcel(list, "cost");
    }

    /**
     * 获取计费详细信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:cost:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tCostService.selectTCostById(id));
    }

    /**
     * 新增计费
     */
    @PreAuthorize("@ss.hasPermi('mahjong:cost:add')")
    @Log(title = "计费", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCost tCost)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tCost.setBak2(shopId.toString());

        return toAjax(tCostService.insertTCost(tCost));
    }

    /**
     * 修改计费
     */
    @PreAuthorize("@ss.hasPermi('mahjong:cost:edit')")
    @Log(title = "计费", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCost tCost)
    {
        return toAjax(tCostService.updateTCost(tCost));
    }

    /**
     * 删除计费
     */
    @PreAuthorize("@ss.hasPermi('mahjong:cost:remove')")
    @Log(title = "计费", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tCostService.deleteTCostByIds(ids));
    }
}
